package com.stack.knowyouweb.controller;

import com.google.gson.Gson;
import com.stack.knowyoubean.bean.RepJson;
import com.stack.knowyoubase.constant.GlobalConst;
import com.stack.knowyouservice.service.FileService;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件相关响应层
 *
 * @author stack
 */
@RestController
@Scope("singleton")
@RequestMapping("/api")
public class FileController {

    @Resource
    private FileService fileService;
    @Resource
    private Gson gson;

    @PostMapping("/imgUpload")
    public String imgUpload(@RequestParam(value = "editormd-image-file", required = false) MultipartFile file, HttpSession session, Authentication authentication) {
        Integer sid = (Integer) session.getAttribute("courseid");
        RepJson<String> repJson = new RepJson<>();
        if (sid == null) {
            repJson.setCode(GlobalConst.ERROR_CODE);
            repJson.setMsg("上传失败，没有选择课程");
        } else {
            String fileUrl = fileService.imgUpload(file, sid, authentication);
            if (StringUtils.isEmpty(fileUrl)) {
                repJson.setCode(GlobalConst.ERROR_CODE);
                repJson.setMsg("上传失败");
            } else {
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setMsg("上传成功");
                repJson.setData(fileUrl);
            }
        }
        return gson.toJson(repJson);
    }


    private void creatRepJson(RepJson<Map<String, String>> repJson, String fileUrl) {
        Map<String, String> videoMap = new HashMap<>(2);
        videoMap.put("type", GlobalConst.ERROR_TYPE);
        if (StringUtils.isEmpty(fileUrl)) {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setMsg("上传失败");
        } else {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setMsg("上传成功");
            videoMap.put("type", GlobalConst.SUCCESS_TYPE);
            videoMap.put("url", fileUrl);
            repJson.setData(videoMap);
        }
    }

    @PostMapping("/avatarUpload")
    public String avatarUpload(@RequestParam(value = "file", required = false) MultipartFile file, Authentication authentication) {
        RepJson<Map<String, String>> repJson = new RepJson<>();
        String fileUrl = fileService.avatarUpload(file, authentication.getName());
        creatRepJson(repJson, fileUrl);
        return gson.toJson(repJson);
    }


    @PostMapping("/videoUpload")
    public String videoUpload(@RequestParam(value = "file", required = false) MultipartFile file, HttpSession session) {
        Integer sid = (Integer) session.getAttribute("courseid");
        RepJson<Map<String, String>> repJson = new RepJson<>();
        if (sid == null) {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setMsg("上传失败，没有选择课程");
        } else {
            String fileUrl = fileService.videoUpload(file, sid);
            creatRepJson(repJson, fileUrl);
        }
        return gson.toJson(repJson);
    }


}
